from src.choice_api.utils import data_str2timestamp, sort_by_first_element


def parse_composite_consume_record(data, columns_lst):
    if data is None or "list" not in data:
        return None

    data_lst = data["list"]
    if not data_lst:
        data_lst = []

    # 真实要获取的字段
    columns_lst = ['value']

    type_mapping = {1: '工业用水', 2: '生活用水'}

    # 单表时序指标形式
    res_columns = []
    res_data = {}

    for row in data_lst:
        if row["type"] in type_mapping:
            t = type_mapping[row["type"]]
        else:
            t = row["type"]

        label = f"水耗 {t}"
        if label not in res_columns:
            res_columns.append(label)
            res_data[label] = []

        res_data[label].append([data_str2timestamp(row["recordTime"]), row["value"]])

    for column in res_columns:
        res_data[column] = sort_by_first_element(res_data[column])

    return {
        "res_columns": res_columns,
        "res_data": res_data
    }
